from time import sleep,ctime
import logging
import _thread

logging.basicConfig(level=logging.INFO)
loops = [2,4,6]
def loop(nloop,nsec,lock):
    logging.info('start loop'+str(nloop)+' at'+ctime())
    sleep(nsec)
    print(nsec)
    logging.info('end loop '+str(nloop)+'at' + ctime())
    lock.release()

def main():
    logging.info('start all loop'+ctime())
    locks = [] #放置所有loop的锁，要是检测到都释放了就退出
    nloops = range(len(loops))
    for i in nloops:
        lock = _thread.allocate_lock() #声明一个锁
        lock.acquire()                 #锁住，加锁操作
        locks.append(lock)             #locks追加了所有的锁
    for i in nloops:
        _thread.start_new_thread(loop,(i,loops[i],locks[i]))   #起一个新线程
    for i in nloops:
        while locks[i].locked():pass   #判断锁是否被释放，如果锁则进行循环，解锁则释放循环
    logging.info('end all loop' + ctime())

if __name__ == '__main__':
    main()